﻿
// comAPP2Doc.cpp: CcomAPP2Doc 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "comAPP2.h"
#endif

#include "comAPP2Doc.h"

#include <propkey.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CcomAPP2Doc

IMPLEMENT_DYNCREATE(CcomAPP2Doc, CDocument)

BEGIN_MESSAGE_MAP(CcomAPP2Doc, CDocument)
END_MESSAGE_MAP()


// CcomAPP2Doc 构造/析构

CcomAPP2Doc::CcomAPP2Doc() noexcept
{
	// TODO: 在此添加一次性构造代码

}

CcomAPP2Doc::~CcomAPP2Doc()
{
}

//对应新建文件操作
BOOL CcomAPP2Doc::OnNewDocument()
{
	ClearFigureBuffer();
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: 在此添加重新初始化代码
	// (SDI 文档将重用该文档)


	return TRUE;
}


//增加图形到缓冲区
BOOL CcomAPP2Doc :: AddOneFigureToBuffer(CFigure* m_Figure)
{
	FigureBuffer.Add(m_Figure);
	return TRUE;
}

//清空缓冲区
VOID  CcomAPP2Doc :: ClearFigureBuffer()
{
	int iCount = FigureBuffer.GetSize();
	for (int i = 0; i < iCount; i++)
		delete FigureBuffer[i];
	FigureBuffer.RemoveAll();
}

typedef struct tagFileHead
{
	int  version;
	CHAR szPswd[16];
	int figCount;    //为了便于读取，存的时候往往带上图形个数。
}FILEHEAD;



// CcomAPP2Doc 序列化

void CcomAPP2Doc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: 在此添加存储代码
		FILEHEAD fileHead = { 0,"@lzj2023",0 };
		fileHead.figCount = FigureBuffer.GetSize();
		//写入文件头
		ar.Write(&fileHead, sizeof(FILEHEAD));
		//写入内存数据
		for (int i = 0; i < fileHead.figCount; i++)
		{
			//写入图元类型
			ar << FigureBuffer[i]->Figure;
			//写入图元具体数据
			FigureBuffer[i]->save(ar);
		}
	}
	else
	{
		// TODO: 在此添加加载代码
		FILEHEAD fileHead = { 0,"@lzj2023",0 };
		FILEHEAD thisfileHead;
		ar.Read(&thisfileHead, sizeof(FILEHEAD));
	    //验证文件头是否正确
		if (strcmp(thisfileHead.szPswd, fileHead.szPswd) == 0)
		{
			for (int i = 0; i < thisfileHead.figCount; i++)
			{
				int FigureType = 0;
				CFigure* ThisFigure = NULL;
				//先读文件类型
				ar >> FigureType;
				//根据类型再读文件数据
				switch (FigureType)
				{
				case F_LINE:     ThisFigure = new CMyLine();       break;
				case F_RECT:     ThisFigure = new CMyRect();       break;
				case F_ELLIPSE:  ThisFigure = new CMyElli();       break;
				case F_TEXT:     ThisFigure = new CMyText();       break;
				default:break;
				}

				if (ThisFigure)
				{
					ThisFigure->load(ar);
					FigureBuffer.Add(ThisFigure);
				}

			}
			
		}
		SelectFigureIndex = -1;  //清除选择

	}
}

#ifdef SHARED_HANDLERS

// 缩略图的支持
void CcomAPP2Doc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// 修改此代码以绘制文档数据
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// 搜索处理程序的支持
void CcomAPP2Doc::InitializeSearchContent()
{
	CString strSearchContent;
	// 从文档数据设置搜索内容。
	// 内容部分应由“;”分隔

	// 例如:     strSearchContent = _T("point;rectangle;circle;ole object;")；
	SetSearchContent(strSearchContent);
}

void CcomAPP2Doc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl *pChunk = nullptr;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != nullptr)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CcomAPP2Doc 诊断

#ifdef _DEBUG
void CcomAPP2Doc::AssertValid() const
{
	CDocument::AssertValid();
}

void CcomAPP2Doc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CcomAPP2Doc 命令

//打开一个文件
BOOL CcomAPP2Doc::OnOpenDocument(LPCTSTR lpszPathName)
{
	ClearFigureBuffer();
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	// TODO:  在此添加您专用的创建代码

	return TRUE;
}
